package com.woniuxy.authapp.app.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.woniuxy.authapp.client.AuthClient;
import com.woniuxy.authapp.client.param.Oauth2TokenInfo;
import com.woniuxy.authapp.client.param.loginInfo;
import com.woniuxy.authapp.common.constant.AuthConstant;
import com.woniuxy.authapp.common.exception.MyException.AuthException;
import com.woniuxy.authapp.common.exception.MyExceptionCode.AuthExceptionCode;
import com.woniuxy.authapp.service.dto.Oauth2TokenDto;
import com.woniuxy.commons.util.Result;
import com.woniuxy.commons.util.WebUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.util.HashMap;

@RestController
public class AuthController implements AuthClient {

    @Autowired
    private TokenEndpoint tokenEndpoint;


    public Result<Oauth2TokenInfo> postAccessToken(
            @RequestParam String grant_type, @RequestParam String client_id,
            @RequestParam String client_secret,
            @RequestParam(required = false) String refresh_token,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String password) throws HttpRequestMethodNotSupportedException {
        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("grant_type",grant_type);
        parameters.put("client_id",client_id);
        parameters.put("client_secret",client_secret);
        //如果存在则不放入
        parameters.putIfAbsent("refresh_token",refresh_token);
        parameters.putIfAbsent("username",username);
        parameters.putIfAbsent("password",password);
        HttpServletRequest request1 = WebUtil.getRequest();
        OAuth2AccessToken auth2AccessToken = tokenEndpoint.postAccessToken(request1.getUserPrincipal(), parameters).getBody();
        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.builder()
                .token(auth2AccessToken.getValue())
                .refreshToken(auth2AccessToken.getRefreshToken().getValue())
                .expiryIn(auth2AccessToken.getExpiresIn())
                .tokenHead(AuthConstant.JWT_TOKEN_PREFIX).build();
        Oauth2TokenInfo oauth2TokenInfo = BeanUtil.copyProperties(oauth2TokenDto, Oauth2TokenInfo.class);
        return Result.getInstance(Oauth2TokenInfo.class).setData(oauth2TokenInfo).setMsg("成功").setCode(200);
    }


//    @Override
//    public Result login(loginInfo loginInfo) throws HttpRequestMethodNotSupportedException {
//        System.out.println("1111111111");
//        if(StrUtil.isEmpty(loginInfo.getUsername()) || StrUtil.isEmpty(loginInfo.getPassword())){
//            throw new AuthException(AuthExceptionCode.USERNAME_OR_PASSWORD_ERROR);
//        }
//        String client_id = AuthConstant.ADMIN_CLIENT_ID;
//        String grant_type = "password";
//        String client_secret = "123456";
//        String refresh_token = null;
//        String user_name = loginInfo.getUsername();
//        String pass_word = loginInfo.getPassword();
//        HttpServletRequest request = WebUtil.getRequest();
//
//        Result<Oauth2TokenInfo> postAccessToken = this.postAccessToken( grant_type, client_id, client_secret, null, user_name, pass_word);
//        return Result.getInstance(Result.class).setData(postAccessToken);
//
//    }

    @Override
    public Result logout() {
        System.out.println("111111111");
        return Result.getInstance().setCode(200).setMsg("已退出登录");
    }
}
